using System;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using System.Diagnostics;
using System.Windows.Forms; 
using System.Runtime.InteropServices;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using OfficeCore = Microsoft.Office.Core;
using SharePointExplorer;

namespace OutlookSharePointExplorer
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ThisApplication
    {
        /// <summary>
        /// The inspectors Collection
        /// </summary>
        MSOutlook.InspectorsClass myInspectors = null;

        /// <summary>
        /// The Inspector Wrapper
        /// </summary>
        ActiveInspectors myActiveInspectors = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThisApplication_Startup(object sender, System.EventArgs e)
        {
            myActiveInspectors = new ActiveInspectors();

            // How to wrap an Outlook Inspector
            // Get the Outlook Inspectors Collection
            myInspectors = (MSOutlook.InspectorsClass)this.Inspectors;

            // Register for the NewInspector event.
            // Doesn't work for this.Inspectors.....
            myInspectors.NewInspector += new MSOutlook.InspectorsEvents_NewInspectorEventHandler(myInspectors_NewInspector);

            //MessageBox.Show("Inspectors : "+ this.Inspectors.Count);
        }

        /// <summary>
             /// 
             /// </summary>
             /// <param name="sender"></param>
             /// <param name="e"></param>
        private void ThisApplication_Shutdown(object sender, System.EventArgs e)
        {
            //MessageBox.Show("Inspectors : " + this.Inspectors.Count);

            if (myInspectors != null)
            {
                // Unregister for NewInspector Event
                myInspectors.NewInspector -= new MSOutlook.InspectorsEvents_NewInspectorEventHandler(myInspectors_NewInspector);

                // Release Reference to COM Object
                Marshal.ReleaseComObject(myInspectors);
            }

            GC.Collect();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Inspector"></param>
        void myInspectors_NewInspector(MSOutlook.Inspector Inspector)
        {
            // Get the current Item behind the Inspector
            object item = Inspector.CurrentItem;

            OutlookInspector TheInspector = null;

            // Check the ItemType to open the correct Item
            if (item is MSOutlook.MailItem)
            {
                TheInspector = new OutlookMailItem(item);
            }
            else if (item is MSOutlook.AppointmentItem)
            {
                TheInspector = new OutlookAppointmentItem(item);
            }
            else if (item is MSOutlook.MeetingItem)
            {
                TheInspector = new OutlookMeetingItem(item);
            }

            // If we have one of our Items add it to the ActiveInspectors Collection
            if (TheInspector != null)
            {
                myActiveInspectors.Add(TheInspector);
            }
        }


        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisApplication_Startup);
            this.Shutdown += new System.EventHandler(ThisApplication_Shutdown);
        }

        #endregion
    }
}
